//
//  MainViewController.swift
//  我的微博
//
//  Created by H on 15/7/28.
//  Copyright © 2015年 H. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
//        addChildViewController()
        addChildViewControllers()
//        setupconposeButton()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //添加所有的控制器
    private func addChildViewControllers() {
    addChildViewController(HomeTableViewController(), title: "首页", imageNamed: "tabbar_home")
    addChildViewController(MessageTableViewController(), title: "消息", imageNamed: "tabbar_message_center")
    addChildViewController(UIViewController())
    addChildViewController(DiscoverTableViewController(), title: "发现", imageNamed: "tabbar_discover")
    addChildViewController(ProfileTableViewController(), title: "我", imageNamed: "tabbar_profile")
   
    
    }
    //设置添加按钮
    private func  setupconposeButton(){
    
    let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
    let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2*w, 0)
    }
    //按钮的点击事件
//    private 添加之后之后只能在当前类使用
   func clickComposedButton(){
    
    print(".....")
    
    }
    //按钮添加
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //此时tabbarbutton以创建
        setupconposeButton()
    }
    
    
    
    //添加一个控制器
    private func addChildViewController(vc:UIViewController,title:String,imageNamed:String) {
        tabBar.tintColor = UIColor.orangeColor()
        let vc = HomeTableViewController()
        vc.title = title
        vc.tabBarItem.image = UIImage(named:imageNamed)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)

       
    }
    //懒加载控件
    private lazy var composeButton: UIButton = {
    () -> UIButton in
    let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
      self.tabBar.addSubview(button)
        button.addTarget(self, action:"clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
    return button
    
    
    }()
    
    
    
    }

